package com.ghck.server.controller;

import java.security.Principal;
import java.security.KeyStore.PrivateKeyEntry;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ghck.server.GhckAplication;
import com.ghck.server.dao.MakerMapper;
import com.ghck.server.exception.ErrorConstant;
import com.ghck.server.exception.GhckException;
import com.ghck.server.model.Maker;
import com.ghck.server.model.request.MakerReq;
import com.ghck.server.model.response.Result;
import com.ghck.server.model.response.ResultList;
import com.ghck.server.model.response.ResultObject;
import com.ghck.server.security.AjkUserDetails;
import com.ghck.server.service.MakerService;
import com.ghck.server.utils.AjkUtil;
import com.ghck.server.utils.FileStoreUtil;
import com.ghck.shop.model.Seller;
import com.ghck.shop.service.SellerService;


@RestController
@RequestMapping("/maker")
public class MakerController {

	private final static Logger LOGGER = LoggerFactory.getLogger(MakerController.class);
	
	@Autowired
	private MakerService makerService;
	@Autowired
	private MakerMapper makerMapper;
	@Autowired
	private SellerService sellerService;
	
	
	@RequestMapping(value="/myMaker",method = RequestMethod.GET)
	@PreAuthorize(GhckAplication.shop_maker_user)
	public ResultObject<Map<String,Object>> myMaker(Principal principal){
		AjkUserDetails user = AjkUtil.getUserDetails(principal);
		ResultObject<Map<String,Object>> result = new ResultObject<Map<String,Object>>();
		Maker dbMaker = makerMapper.findMakerByUserId(user.getUserId());
		Map<String,Object> data = new HashMap<>();
		data.put("maker", dbMaker);
		Seller seller = sellerService.findSellerByUsrId(user.getUserId());
		data.put("seller", seller);
		result.setDomain(data);
		return result;
	}
	
	@RequestMapping(value="/addMaker",method = RequestMethod.POST,consumes = "multipart/form-data")
	@PreAuthorize(GhckAplication.user)
	public Result addMaker(Principal principal,MakerReq makerReq){
		LOGGER.info("get a add maker request:{}", JSONObject.toJSONString(makerReq));
		AjkUserDetails user = AjkUtil.getUserDetails(principal);
		makerReq.setUserId(user.getUserId());
		Result result = new Result();
		if(StringUtils.isEmpty(makerReq.getContactName())){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"联系人不能为空");
			throw exception;
		}
		if(StringUtils.isEmpty(makerReq.getContactPhone())){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"联系电话不能为空");
			throw exception;
		}
		if(StringUtils.isEmpty(makerReq.getCardNum())){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"身份证号不能为空");
			throw exception;
		}
		if(makerReq.getProvinceId() == null || makerReq.getProvinceId() <= 0){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"申请区域省份不能为空");
			throw exception;
		}
		if(makerReq.getCityId() == null || makerReq.getCityId() <= 0){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"申请区域城市不能为空");
			throw exception;
		}
		if(makerReq.getDistrictId() == null || makerReq.getDistrictId() <= 0){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"申请区域区县不能为空");
			throw exception;
		}
		if(makerReq.getCardFrontFile() == null){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"身份证正面照不能为空");
			throw exception;
		}
		if(makerReq.getCardBackFile() == null){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"身份证反面照不能为空");
			throw exception;
		}
		if(makerReq.getBusinessLicenceFile() == null){
			GhckException exception = new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(),"营业执照不能为空");
			throw exception;
		}
		if(makerReq.getCardFrontFile() != null){
			String fileDbName = FileStoreUtil.storeFile(makerReq.getCardFrontFile(), "maker-request");
			makerReq.setCardFront(fileDbName);
		}
		if(makerReq.getCardBackFile() != null){
			String fileDbName = FileStoreUtil.storeFile(makerReq.getCardBackFile(), "maker-request");
			makerReq.setCardBack(fileDbName);
		}
		if(makerReq.getBusinessLicenceFile() != null){
			String fileDbName = FileStoreUtil.storeFile(makerReq.getBusinessLicenceFile(), "maker-request");
			makerReq.setBusinessLicence(fileDbName);
		}
		LOGGER.info("convert maker request:{}", JSONObject.toJSONString(makerReq));
		makerService.addMaker(makerReq);
		return result;
	}
	
	@RequestMapping(value="/makerPass",method = RequestMethod.POST)
	@PreAuthorize(GhckAplication.admin)
	public Result makerPass(Principal principal,@RequestBody Maker maker){
		Result result = new Result();
		makerService.makerPass(maker);
		return result;
	}
	
	@RequestMapping(value="/makerReject",method = RequestMethod.POST)
	@PreAuthorize(GhckAplication.admin)
	public Result makerReject(Principal principal,@RequestBody Maker maker){
		Result result = new Result();
		makerService.makerReject(maker);
		return result;
	}
	@RequestMapping(value="/list",method = RequestMethod.POST)
	@PreAuthorize(GhckAplication.admin)
	public ResultList<Maker> listMakers(Principal principal,
			Maker maker,
			@RequestParam(defaultValue = "0") Integer start,
            @RequestParam(defaultValue = "20") Integer limit){
		LOGGER.info("查询创客列表,params:{},start:{},limit:{}", JSONObject.toJSONString(maker),start,limit);
		ResultList<Maker> result = new ResultList<Maker>();
		List<Maker> list = makerService.listMakers(maker, start, limit);
		result.setItemList(list);
		return result;
	}
}
